package com.lai.hospital.handler;

import com.alibaba.fastjson.JSON;
import com.lai.hospital.constant.SecurityConst;
import com.lai.hospital.dao.UserAuthDao;
import com.lai.hospital.dto.UserDetailDTO;
import com.lai.hospital.dto.UserInfoDTO;
import com.lai.hospital.entity.UserAuth;
import com.lai.hospital.service.RedisService;
import com.lai.hospital.utils.BeanCopyUtils;
import com.lai.hospital.utils.JwtTokenUtils;
import com.lai.hospital.utils.JwtUtils;
import com.lai.hospital.utils.UserUtils;
import com.lai.hospital.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

import static com.lai.hospital.constant.CommonConst.APPLICATION_JSON;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.handler
 * @Description: 登录成功处理器(重要)
 * 授权成功之后颁发token
 * @date Date : 2022年11月17日 11:41
 */
@Component
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private UserAuthDao userAuthDao;

    @Autowired
    private RedisService redisService;


    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        UserInfoDTO userLoginDTO = BeanCopyUtils.copyObject(UserUtils.getLoginUser(), UserInfoDTO.class);
        //String parameter = httpServletRequest.getParameter("remember-me");
        //生成token    parameter.equalsIgnoreCase("1") || parameter.equalsIgnoreCase("true")
        String token = jwtUtils.generateJwt(UserUtils.getLoginUser(),true);

        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("data",userLoginDTO);
        httpServletResponse.setContentType(APPLICATION_JSON);
        httpServletResponse.getWriter().write(JSON.toJSONString(Result.ok(map)));
//        UserInfoDTO userLoginDTO = BeanCopyUtils.copyObject(UserUtils.getLoginUser(), UserInfoDTO.class);
//        httpServletResponse.setContentType(APPLICATION_JSON);
//        httpServletResponse.getWriter().write(JSON.toJSONString(Result.ok(userLoginDTO)));
        // 更新用户ip，最近登录时间
        updateUserInfo();
        //把token存入redis
        //redisService.set(userLoginDTO.getId().toString(), SecurityConst.TOKEN_PREFIX + token);
    }

    /**
     * 更新用户信息
     */
    @Async
    public void updateUserInfo() {
        UserAuth userAuth = UserAuth.builder()
                .id(UserUtils.getLoginUser().getId())
                .ipAddress(UserUtils.getLoginUser().getIpAddress())
                .ipSource(UserUtils.getLoginUser().getIpSource())
                .lastLoginTime(UserUtils.getLoginUser().getLastLoginTime())
                .build();
        userAuthDao.updateById(userAuth);
    }
}
